package com.yxq.mdt.app.modules.bottomsheet;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.dialog.ShareRQDialog;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 保存图片弹出框
 */
public class SavePictureSheetDialog extends BottomSheetDialogFragment {
    Unbinder unbinder;
    private String url;
    Context context;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    public SavePictureSheetDialog(Context context, String url) {
        this.context = context;
        this.url = url;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_dialog_save_picture, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @OnClick({R.id.dialog_tv_save, R.id.dialog_tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_tv_save:
                dismiss();
                Glide.with(getContext()).asBitmap()
                        .load(url).into(new SimpleTarget<Bitmap>() {

                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        ShareRQDialog.saveBitmap2file(resource, context);
                    }
                });
                break;
            case R.id.dialog_tv_cancel:
                dismiss();
                break;
        }
    }

}
